#include "stdafx.h"
#include "GUI.h"
#include "..\..\Global\globaldef.h"


GUI::State GUI::Main::Renderer::g_MainState = GUI::State::CLOSED; 
std::vector<GUI::Components::Button> GUI::Main::Renderer::g_Buttons; 
//Render::tResource* GUI::Main::Renderer::g_pButtonSet = NULL;
GUI::tStamp GUI::Main::Renderer::m_MainStamp = {0,0,0,0};
D3DXVECTOR2 GUI::Main::Renderer::Position = D3DXVECTOR2(0,0);
float GUI::Main::Renderer::g_AnimateOffset = 0.0f;
Render::tResource* GUI::Main::Renderer::g_pBackground = NULL;

#define MAIN_ANIM_SPEED 10.0f

float GUI::Main::Renderer::AnimateDistance() {

	UINT h = 0;
	Render::Singleton()->GetTextureInfo(GUI::Main::Renderer::g_pBackground, NULL, &h);
	D3DXVECTOR2 t;
	GUI::ScaleToWindow(0,h, &t);

	return t.y;
}

void GUI::Main::Renderer::Update() {
	
	switch(GUI::Main::Renderer::g_MainState) {

		case GUI::OPENING:
		case GUI::CLOSING: {

			const float target = (GUI::Main::Renderer::g_MainState == GUI::OPENING) ? 0.0f : -GUI::Main::Renderer::AnimateDistance();
			const float delta = (target - GUI::Main::Renderer::g_AnimateOffset) * 0.25f;
			const float blip = 0.001f;

			GUI::Main::Renderer::g_AnimateOffset += delta;
			
			if((GUI::Main::Renderer::g_MainState == GUI::OPENING) && (GUI::Main::Renderer::g_AnimateOffset >= target - blip)) {
				
				GUI::Main::Renderer::g_MainState = GUI::OPEN;
				GUI::Main::Renderer::g_AnimateOffset = target;
				GUI::PokeMouse();
			}
			else if((GUI::Main::Renderer::g_MainState == GUI::CLOSING) && (GUI::Main::Renderer::g_AnimateOffset <= target + blip)) {
				
				GUI::Main::Renderer::g_MainState = GUI::CLOSED;
				GUI::Main::Renderer::g_AnimateOffset = target;
				GUI::PokeMouse();
			}

			FOREACH(GUI::Components::Button, i, g_Buttons)
				i->Position.y = Position.y + GUI::Main::Renderer::g_AnimateOffset;

	   }
	   break;
		
		case GUI::OPEN:
			//GUI::Main::Renderer::Update();

		case GUI::CLOSED:
			break;
	}

}
void GUI::Main::Renderer::OnToggle(void*, int) {

	GUI::Main::Renderer::g_MainState = (GUI::Main::Renderer::g_MainState == GUI::State::CLOSED ||
		GUI::Main::Renderer::g_MainState == GUI::State::CLOSING) ?  GUI::State::OPENING : 
		GUI::State::CLOSING;
}

void GUI::Main::Renderer::Draw() {

	if(GUI::Main::Renderer::g_MainState != GUI::CLOSED) {
		
		D3DXVECTOR2 render_position = D3DXVECTOR2(0, g_AnimateOffset) + Position;
		Render::Singleton()->DrawSprite(GUI::Main::Renderer::g_pBackground, render_position, D3DXVECTOR2(1,1), NULL);
		
		//GUI::Main::Renderer::Draw();
		/*FOREACH(GUI::Components::Button, i, g_Buttons)
			i->Draw();

		FOREACH(GUI::Components::Button, i, g_Buttons)
			i->Draw();*/

	//	GUI::Main::Renderer::Draw();
	}
}

void GUI::Main::Renderer::OnMouseEvent(const float x, const float y, const GUI::MouseButton left, const GUI::MouseButton right) {
	
	switch(GUI::Main::Renderer::g_MainState) {

		case GUI::State::OPEN: {

			FOREACH(GUI::Components::Button, i, g_Buttons) {
				
				i->OnMouseEvent(x,y,left,right);
			}

		}break;
	}
}

bool GUI::Main::Renderer::Create() {

	Render::tResource* pBackground = NULL;
	/*std::vector<GUI::Components::Button> buttons;*/

	try {

		if(!Render::Singleton()->CreateTexture("GUI\\lv_viewer.png", &pBackground))
			throw tException("Failed to create set texture");

		Release();

		/*for(uint i = 0; i < 4; i++) {

			RECT buttonTemplate;
			GUI::StampCreate(i * 104, 0, 104, 92, &buttonTemplate);
			
			GUI::Components::Button button = GUI::Components::Button();
			if(!button.Create( i == 0 ? "Viewer" :
				i == 1 ? "Network" :
				i == 2 ? "Renderer" :
				"Help"))
				continue;

			GUI::ScaleToWindow(i * 103, 25, &button.Position);
			GUI::Main::Renderer::g_Buttons.push_back(button);
		}*/

		GUI::Main::Renderer::g_MainState = GUI::State::CLOSED;
		GUI::Main::Renderer::g_pBackground = pBackground;
		GUI::ScaleToWindow(100, 22, &GUI::Main::Renderer::Position);
		GUI::Main::Renderer::g_AnimateOffset = -GUI::Main::Renderer::AnimateDistance();
		//RECT stamp;
		//stamp.left = 0;
		//stamp.top = 0;
		//if(!Render::Singleton()->GetTextureInfo(pBackground, (UINT*)&stamp.right, (UINT*)&stamp.bottom))
		//	throw tException();

		//stamp.bottom /= 3;
		//GUI::StampGetPercentage(&stamp, &GUI::Main::Renderer::m_MainStamp);
		//GUI::Main::Renderer::g_AnimateOffset = GUI::Height(&GUI::Main::Renderer::m_MainStamp);
		//GUI::Main::Renderer::g_pBackground = pBackground;

		//GUI::Main::Renderer::g_MainState = GUI::CLOSED;
		//
		//GUI::ScaleToWindow(0, 25, &GUI::Main::Renderer::Position);
		//GUI::Main::Renderer::g_AnimateOffset = -GUI::Main::Renderer::AnimateDistance();
		//FOREACH(GUI::Components::Button, i, g_Buttons)
		//	i->Position.y = Position.y + GUI::Main::Renderer::g_AnimateOffset;

		return true;

	}catch(tException e) {

		Render::Singleton()->ReleaseResource(&pBackground);
		Release();

		std::string message;
		tException::GetMessage(&e, message);
		return false;
	}
}

bool GUI::Main::Renderer::Release() {

	/*FOREACH(GUI::Components::Button, i, GUI::Main::Renderer::g_Buttons)
		i->Release();
	GUI::Main::Renderer::g_Buttons.clear();
	ZeroMemory(&GUI::Main::Renderer::m_MainStamp, sizeof(RECT));*/
	Render::Singleton()->ReleaseResource(&GUI::Main::Renderer::g_pBackground);
	return true;
}